package com.etc.zzsys.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.zzsys.dao.AnswerDao;
import com.etc.zzsys.dao.impl.AnswerDaoImpl;
import com.etc.zzsys.entity.Answer;
import com.etc.zzsys.entity.Question;
import com.etc.zzsys.service.QuestionService;
import com.etc.zzsys.service.impl.QuestionServiceimpl;
import com.etc.zzsys.util.PageData;
import com.google.gson.Gson;

/**
 * Servlet implementation class QianTaiServlet
 */
@WebServlet("/qt")
public class QianTaiServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public QianTaiServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String op="";
		if(request.getParameter("op")!=null) {
			op=request.getParameter("op");
		}
		int page=1;
		int pageSize=6;
		if(request.getParameter("page")!=null) {
			page=Integer.valueOf(request.getParameter("page"));
		}
		if(request.getParameter("pageSize")!=null) {
			pageSize=Integer.valueOf(request.getParameter("pageSize"));
		}
		if(op.equals("")) {
//			AnswerDao ad=new AnswerDaoImpl();
//			PageData<Answer> pdata=ad.queryAnswerByPage(1,6,"");
//			request.setAttribute("answerList",pdata.getData());
			
			QuestionService qs=new QuestionServiceimpl();
			PageData<Question> pdata=qs.queryQuestionByPage(page, pageSize,"");
			request.setAttribute("pdata", pdata);
			request.setAttribute("qList",pdata.getData());
			request.setAttribute("lqList", qs.getLastQuestion(6));
			request.getRequestDispatcher("qindex.jsp").forward(request, response);
		
		}
		if("qajax".equals(op)) {
			QuestionService qs=new QuestionServiceimpl();
			PageData<Question> pdata=qs.queryQuestionByPage(page, pageSize,"");
			Gson gson=new Gson();
			String jsonStr=gson.toJson(pdata.getData());
			response.setContentType("text/json");
			PrintWriter out=response.getWriter();
		}
		if("search".equals(op)) {
			String key=request.getParameter("s");
			QuestionService qs=new QuestionServiceimpl();
			PageData<Question> pdata=qs.queryQuestionByPage(page, pageSize,key);
			if(pdata!=null) {
				request.setAttribute("qList",pdata.getData());
				request.setAttribute("pdata",pdata);
			}else {
				request.setAttribute("qList",null);
				request.setAttribute("pdata",null);
			}
			
			request.getRequestDispatcher("qQuestionList.jsp").forward(request, response);;
		}
		if("qDetail".equals(op)) {
			int qId=Integer.valueOf(request.getParameter("id"));
			QuestionService qs=new QuestionServiceimpl();
			Question question=qs.getQuestionById(qId);
			request.setAttribute("question", question);
			request.getRequestDispatcher("questionDetail.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
